﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BuXiaChu.OrderDishes.BLL.WebRuntime;
using System.Web.Services;
using BuXiaChu.OrderDishes.BLL;
using BuXiaChu.OrderDishes.Model;

namespace BuXiaChu.OrderDishes.Web.CallCenter.Order
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class GetUserInfo : HttpHandlerBase, IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            base.Init(context);

            UserInfo();
        }

        private void UserInfo()
        {
            #region
            AjaxActionResult result = new AjaxActionResult();
            var callerId = GetFormValue("callerId");
            if (!string.IsNullOrEmpty(callerId))
            {
                int pageCount = 0;
                var sqlWhere = " Telephone = '" + callerId + "' or MobileTelephone='" + callerId + "'";
                List<UserInfoModel> userInfoList = UserInfoManager.Search(sqlWhere, 0, 0, out pageCount);
                if (userInfoList.Count > 0)
                {
                    result.success = true;
                    sqlWhere = " userId = " + userInfoList[0].UserId;
                    List<DeliveryAddressModel> addressList = DeliveryAddressManager.Search(sqlWhere, 0, 0, out pageCount);
                    
                    #region
                    List<Object> addrList = new List<object>();
                    if (addressList.Count > 0)
                    {
                        addressList.ForEach(item =>
                        {
                            addrList.Add(new
                            {
                                id= item.Id,
                                cityId = item.CityId,
                                areaId = item.AreaId,
                                address = item.Address,
                                name = item.Name,
                                gender = item.Gender,
                                invoiceTitle = item.InvoiceTitle,
                                mobile = item.MobileTelephone,
                                tel = item.Telephone
                            });
                        });

                    }
                    #endregion
                    
                    result.obj = new
                    {
                        userInfo = new
                        {
                            loginName = userInfoList[0].LoginName,
                            userId = userInfoList[0].UserId,
                            gender = userInfoList[0].Gender,
                            name = userInfoList[0].Name
                        },
                        addressList = addrList
                    };
                }
            }

            SerializeDataToResponseStream(result);
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
